package com.yourenbang.lingyun.transaction.web.service.impl.migrate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.parser.ParserConfig;
import com.yourenbang.lingyun.core.util.KLog;
import com.yourenbang.lingyun.core.util.date.LocalDateUtils;
import com.yourenbang.lingyun.core.util.redis.RedisUtils;
import com.yourenbang.lingyun.domain.MerchantSignHjtx;
import com.yourenbang.lingyun.mapper.MerchantSignHjtxMapper;
import com.yourenbang.lingyun.mapper.UserBaseExtMapper;
import com.yourenbang.lingyun.transaction.config.AcmTransactionParams;
import com.yourenbang.lingyun.transaction.web.domain.dto.migrate.kuaishou.HjtxSignDTO;
import com.yourenbang.lingyun.transaction.web.domain.dto.migrate.kuaishou.QueryTableDateDTO;
import com.yourenbang.lingyun.transaction.web.service.migrate.CommonMigrateService;
import com.yourenbang.lingyun.transaction.web.service.migrate.HjtxSignMigrateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Date 2021/3/1 11:40
 * @Created by Jason
 */
@Service
public class HjtxSignMigrateServiceImpl extends AbstractMigrateService implements HjtxSignMigrateService {

    @Autowired
    private RedisUtils redis;
    @Autowired
    private AcmTransactionParams acmTransactionParams;
    @Autowired
    private CommonMigrateService commonMigrateService;
    @Autowired
    private UserBaseExtMapper userBaseExtMapper;
    @Autowired
    private MerchantSignHjtxMapper merchantSignHjtxMapper;

    @Override
    public boolean migrateHjtxSign(QueryTableDateDTO dto) {
        String url = acmTransactionParams.getKaolakuaishouHost();
        String uri = "/query/pay/baseQuery";
        url = url + uri;

        JSONArray jsonArray = commonMigrateService.pullDataFromKS(url, dto.toJSON());
        if (jsonArray.size() == 0) {
            return false;
        }
        //parse（字符串转对象）的时候 设置下划线转驼峰
        ParserConfig.getGlobalInstance().propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;

        List<HjtxSignDTO> hjtxSignDTOS = jsonArray.toJavaList(HjtxSignDTO.class);
        KLog.info("[hjtx签约迁移]查询到的条数为:{}", hjtxSignDTOS.size());
        saveHjtxSignData(hjtxSignDTOS);

        return true;
    }

    private void saveHjtxSignData(List<HjtxSignDTO> hjtxSignDTOS) {
        List<MerchantSignHjtx> merchantSignHjtxes = new ArrayList<>();

        LocalDateTime now = LocalDateTime.now();
        for (HjtxSignDTO hjtxSignDTO : hjtxSignDTOS) {
            MerchantSignHjtx merchantSignHjtx = new MerchantSignHjtx();
            try {
                merchantSignHjtx.setUserBaseId(getUserBaseId(userBaseExtMapper, hjtxSignDTO.getUserId(), redis));
            } catch (Exception e) {
                KLog.warn("迁移hjtx签约失败, 用户id不存在:{}",hjtxSignDTO.getUserId());
            }
            merchantSignHjtx.setIdNumber(hjtxSignDTO.getIdCard());
            merchantSignHjtx.setCreateTime(hjtxSignDTO.getCreateTime() == null ? now
                    : LocalDateUtils.stringToLocalDateTime(hjtxSignDTO.getCreateTime()));
            merchantSignHjtx.setUpdateTime(hjtxSignDTO.getUpdateTime() == null ? now
                    : LocalDateUtils.stringToLocalDateTime(hjtxSignDTO.getCreateTime()));
            merchantSignHjtxes.add(merchantSignHjtx);
        }
        if (merchantSignHjtxes.size() == 0) {
            KLog.info("未查询到hjtx签约数据");
            return;
        }
        if (merchantSignHjtxes.size() > 1500) {
            List<MerchantSignHjtx> merchantSignHjtxes1 = merchantSignHjtxes.subList(0, 1500);
            List<MerchantSignHjtx> merchantSignHjtxes2 = merchantSignHjtxes.subList(1500, merchantSignHjtxes.size());

            int i = merchantSignHjtxMapper.batchInsert(merchantSignHjtxes1);
            KLog.info("[hjtx签约迁移]批量分批插入签约数据结束,第一批条数:{}", i);
            int i2 = merchantSignHjtxMapper.batchInsert(merchantSignHjtxes2);
            KLog.info("[hjtx签约迁移]批量插入签约数据结束,第二批条数:{}", i);
        } else {
            int i = merchantSignHjtxMapper.batchInsert(merchantSignHjtxes);
            KLog.info("[hjtx签约迁移]批量插入签约数据结束,条数:{}", i);
        }

    }
}
